package com.missilike.biz.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.missilike.biz.IMilAccountService;
import com.missilike.core.exception.BizException;
import com.missilike.core.result.BizResultCode;
import com.missilike.domain.response.MilAccountInfo;
import com.missilike.repository.entity.MilAccountThird;
import com.missilike.repository.mapper.MilAccountThirdMapper;
import com.missilike.biz.IMilAccountThirdService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author pengpengli
 * @since 2018-12-19
 */
@Service
public class MilAccountThirdServiceImpl extends ServiceImpl<MilAccountThirdMapper, MilAccountThird> implements IMilAccountThirdService {

    @Lazy
    @Autowired
    private IMilAccountService iMilAccountService;


    public MilAccountInfo getThirdId(String thirdId) {
        MilAccountThird milAccountThird = getOne(new QueryWrapper<MilAccountThird>().lambda().eq(MilAccountThird::getAccountThirdId, thirdId));
        if (milAccountThird == null) {
            throw new BizException(BizResultCode.NO_THIRD);
        }
        return iMilAccountService.getAccount(milAccountThird.getMilAccountId());
    }

    public boolean addEstablishThirdAccount(String thirdId, String accountId,Integer type){
        iMilAccountService.getAccount(accountId);
        MilAccountThird third= getOne(new QueryWrapper<MilAccountThird>().lambda()
                .eq(MilAccountThird::getAccountThirdId, thirdId)
                .eq(MilAccountThird::getAccountThirdType,type)
                .eq(MilAccountThird::getMilAccountId,accountId));
        if (third!=null){
            throw new BizException(BizResultCode.EXIST_THIRD_ID);
        }
        MilAccountThird milAccount= new MilAccountThird();
        milAccount.setAccountThirdId(thirdId);
        milAccount.setMilAccountId(accountId);
        milAccount.setAccountThirdType(type);
        save(milAccount);
        return  true;
    }

    public boolean removeEstablishThirdAccount(String thirdId, String accountId,Integer type){
        iMilAccountService.getAccount(accountId);
        MilAccountThird third= getOne(new QueryWrapper<MilAccountThird>().lambda()
                .eq(MilAccountThird::getAccountThirdId, thirdId)
                .eq(MilAccountThird::getAccountThirdType,type)
                .eq(MilAccountThird::getMilAccountId,accountId));
        if (third==null){
            throw new BizException(BizResultCode.NO_THIRD);
        }
        removeById(third);
        return true;
    }
}
